﻿using System;
using System.Net;
using System.Collections.Specialized;
using System.Text.RegularExpressions;

namespace PicHunter.Task
{
    public class ReadPicUrlsTask //: Task
    {
        private Logging.Log LOG = Logging.Log.Instance;

        public String PageUri { get; set; }
        public StringCollection Result { get; private set; }
        public event TaskCompleted Completed;

        public ReadPicUrlsTask()
        {
            Result = new StringCollection();
        }

        public void DoWork()
        {
            try
            {
                if (PageUri == null) PageUri = "";

                String pageString;

                WebClient client = new WebClient();
                LOG.Info("Downloading ... " + PageUri);
                pageString = client.DownloadString(PageUri);

                // get match
                StringCollection resultList = new StringCollection();

                Regex regexObj = new Regex(@"http://[.\w.-_]*?\.jpg");
                Match matchResult = regexObj.Match(pageString);
                while (matchResult.Success)
                {
                    Result.Add(matchResult.Value);
                    matchResult = matchResult.NextMatch();
                }

                LOG.Info(String.Format("Get {0} picture urls", Result.Count));

                if (Completed != null)
                {
                    Completed();
                }
            }
            catch (System.Exception ex)
            {
            	
            }
        }
    }
}
